package Client.circuitBreaker;

import java.util.HashMap;
import java.util.Map;

public class CircuitBreakerProvider {

    // Map存储每个服务的熔断器实例，key为服务名，value为对应的熔断器实例
    private Map<String, CircuitBreaker> circuitBreakerMap = new HashMap<>();

    // 根据服务名获取对应的熔断器
    public synchronized CircuitBreaker getCircuitBreaker(String serviceName) {
        CircuitBreaker circuitBreaker;

        // 检查是否已经存在该服务的熔断器
        if (circuitBreakerMap.containsKey(serviceName)) {
            circuitBreaker = circuitBreakerMap.get(serviceName);
        } else {
            // 如果不存在，则创建一个新的熔断器实例并添加到map中
            System.out.println("serviceName=" + serviceName + "创建一个新的熔断器实例");
            circuitBreaker = new CircuitBreaker(1, 0.5, 10000);
            circuitBreakerMap.put(serviceName, circuitBreaker);
        }
        return circuitBreaker;
    }
}
